package com.AdvancedDemos.LearnVariousAPI.Object_Objects;

import java.util.Objects;
import java.util.Random;
import java.util.function.Supplier;

public class LearnObjects {
    public static void main(String[] args) {
        //Objects是一个工具类,提供了很多操作对象的静态方法给我们使用,Objects是final修饰的,不可以被继承


        /**
         * 1.Objects.equals(o1,o2);
         */
        String str1 = "itheima";
        String str2 = "itheima";

        System.out.println(str1.equals(str2));
        System.out.println(Objects.equals(str1, str2));


        System.out.println("------------------");
        str1 = null;
//        System.out.println(str1.equals(str2));//之所以重写Object类中的equals方法时,并不是直接调用String类中的equals方法是为了防止出现str1==null的情况,这种情况会出现空指针异常.NullPointerException
        System.out.println(Objects.equals(str1, str2));
        /**
         * Objects类中equals具体实现:
         *
         * public static boolean equals(Object a, Object b) {
         *         return (a == b) || (a != null && a.equals(b));//当中给出了非空校验
         *     }
         */


        /**
         * 2.Objects.isNull(o1);
         */

        System.out.println("------------------");
        System.out.println(Objects.isNull(str1));
        System.out.println(Objects.isNull(str2));
        System.out.println("等价于:");
        System.out.println(str1 == null);
        System.out.println(str2 == null);

        /**
         *   Objects类中isNull具体实现:
         *
         *   public static boolean isNull(Object obj) {
         *         return obj == null;
         *     }
         */


        /**
         * 3.Objects.nonNull(o1);
         */
        System.out.println("------------------");
        System.out.println(Objects.nonNull(str1));
        System.out.println(Objects.nonNull(str2));
        System.out.println("等价于:");
        System.out.println(str1 != null);
        System.out.println(str2 != null);

        /**
         * Objects类中nonNull具体实现:
         *
         * public static boolean nonNull(Object obj) {
         *         return obj != null;
         *     }
         */


        /**
         * 4.public static String toString(Object o, String nullDefault) {
         *         return (o != null) ? o.toString() : nullDefault;
         *     }
         *     如果o!=null直接调o的toString,否则返回指定的字符串
         */
        System.out.println("------------------");
        String s = null;
        System.out.println(Objects.toString(s, "地址为空"));
        s = "hello";
        System.out.println(Objects.toString(s, "地址为空"));
        /**
         *public static String toString(Object o) {
         *         return String.valueOf(o);
         *     }
         */
        double[] doubles = new double[]{1.1, 2.2, 3.3};
        System.out.println(Objects.toString(doubles));


        System.out.println("------------------");
        /**
         * 5.判空函数,空则抛出异常
         * Objects.requireNonNul(...)
         * requireNonNullelse(...)
         * requireNonNullElseGet(...)
         */
        Student stu = new Student("ikun", 1);
//        stu = null;
        //public static <T> T requireNonNull(T obj)
        Student student = Objects.requireNonNull(stu);//非空则返回对象自身,空则抛出空指针异常 NullPointerException
        System.out.println(stu.equals(student));


        System.out.println("------------------");
//        stu = null;
        //public static <T> T requireNonNull(T obj, String message)
        Student student1 = Objects.requireNonNull(stu, "自己定义的异常信息");//NullPointerException: 自己定义的异常信息
        System.out.println(stu.equals(student1));

        System.out.println("------------------");
//        stu = null;
        //public static <T> T requireNonNull(T obj, Supplier<String> messageSupplier)
        Student student2 = Objects.requireNonNull(stu, new Supplier<String>() {
            @Override
            public String get() {
                //可以自己定义异常信息
                String str = "";
                Random random = new Random();
                for (int i = 0; i < 4; i++) {
                    str += random.nextInt(10);
                }
                return str;
            }
        });//NullPointerException: 7028
        System.out.println(stu.equals(student2));


        System.out.println("------------------");
        stu = null;
        //public static <T> T requireNonNullElse(T obj, T defaultObj)
        //如果参数对象1不为null,返回参数对象1自身
        //如果参数对象1为null,返回参数对象2(备用对象)
        //如果参数对象2也为null,抛出空指针异常
        Student student3 = new Student("只因", 2);
//        student3 = null;
        Student student4 = Objects.requireNonNullElse(stu, student3);//NullPointerException
        System.out.println(stu == student4);
        System.out.println(stu == student3);

        System.out.println("------------------");
//        stu = null;
        //public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
        //如果参数对象1不为null,返回参数对象1
        //如果参数对象1为null,返回Supplier中get()获取的Student对象
        //如果get()得到的对象也为null,抛出空指针异常
        Student student5 = Objects.requireNonNullElseGet(stu, new Supplier<Student>() {
            @Override
            public Student get() {
                return new Student("姬霓太美", 777);
//                return null;//NullPointerException
            }
        });
        System.out.println(stu == student5);
        System.out.println(student5);
    }
}

class Student {
    String name;
    int id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", id=" + id + '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Student() {
    }

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }
}
